#include "pointplot.h"

#define XLABEL_WIDTH   (30)
#define XLABEL_HEIGHT  (15)

#define YLABEL_WIDTH   (30)
#define YLABEL_HEIGHT  (15)

#define TOPLABEL_WIDTH  (40)
#define TOPLABEL_HEIGHT (15)

PointPlot::PointPlot(int xWidth, int yHeight, QWidget *parent) : QWidget(parent),
 m_yHeight(yHeight),
 m_xWidth(xWidth)
{
    m_plot = new QCustomPlot(this);
    m_xLabel = new QLabel(this);
    m_yLabel = new QLabel(this);
    m_topLabel = new QLabel(this);

    this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);//隐藏标题设置为顶层显示
    this->setFixedSize(m_xWidth, m_yHeight);
}

PointPlot::~PointPlot()
{

}

void PointPlot::setLabel(PointPlot::PointLabel pointLabel)
{
    //设置标签颜色为白色
    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::white);
    m_xLabel->setPalette(pe);
    m_yLabel->setPalette(pe);
    m_topLabel->setPalette(pe);

    //设置X轴标签
    m_xLabel->setText(pointLabel.xLabel);
    //设置Y轴标签
    m_yLabel->setText(pointLabel.yLabel);
    //设置标题标签
    m_topLabel->setText(pointLabel.topLabel);
}

void PointPlot::setScatterData(const QVector<double> &key, const QVector<double> &value, const QColor &color)
{

    QPen pen;
    pen.setColor(color);
    pen.setWidth(1);
    pen.setStyle(Qt::DotLine);

    m_plot->addGraph();
    m_plot->graph()->setAntialiasedFill(true);
    m_plot->graph()->setLineStyle(QCPGraph::lsNone);
    m_plot->graph()->setPen(pen);
    m_plot->graph()->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDot));
    m_plot->graph()->setData(key, value);
    m_plot->graph()->rescaleAxes(true);
    m_plot->replot();

}

// 清除绘图
void PointPlot::clearPlot()
{
    m_plot->clearGraphs();
    m_plot->replot();
}

void PointPlot::setBkgroundColor( QColor color)
{
    QPalette pal(this->palette());

    //设置背景黑色
    pal.setColor(QPalette::Background, color);
    this->setAutoFillBackground(true);
    this->setPalette(pal);

    QBrush qBrush(QColor(Qt::black));//设置背景色
    //设置画图区域背景色
    m_plot->setBackground(qBrush);
}

void PointPlot::InitScatterPlot()
{
    //设置散点图图形坐标
    m_plot->setGeometry(-10,10, m_xWidth-10, m_yHeight);
    m_yLabel->setGeometry(0, 6, YLABEL_WIDTH, YLABEL_HEIGHT);
    m_yLabel->setAlignment(Qt::AlignCenter);
    m_xLabel->setGeometry(m_xWidth-25, m_yHeight-15, XLABEL_WIDTH, XLABEL_HEIGHT);
    m_xLabel->setAlignment(Qt::AlignLeft);
    m_topLabel->setGeometry(m_plot->width()/2, 0, TOPLABEL_WIDTH, TOPLABEL_HEIGHT);
    m_topLabel->setAlignment(Qt::AlignLeft);

    //设置坐标轴颜色
    m_plot->xAxis->setBasePen(QPen(QColor(Qt::white), 1));
    m_plot->yAxis->setBasePen(QPen(QColor(Qt::white), 1));


    //设置刻尺颜色
    m_plot->xAxis->setTickPen(QPen(QColor(Qt::white), 1));
    m_plot->yAxis->setTickPen(QPen(QColor(Qt::white), 1));
    m_plot->xAxis->setSubTickPen(QPen(QColor(Qt::white), 1));
    m_plot->yAxis->setSubTickPen(QPen(QColor(Qt::white), 1));


    //设置坐标轴标签颜色
    // set blank axis lines:
    m_plot->xAxis->setTicks(false);
    m_plot->yAxis->setTicks(false);
    m_plot->xAxis->setTickLabels(false);
    m_plot->yAxis->setTickLabels(false);

    //设置网格不可见
    m_plot->xAxis->grid()->setVisible(false);
    m_plot->xAxis2->grid()->setVisible(false);
    m_plot->yAxis->grid()->setVisible(false);
    m_plot->yAxis2->grid()->setVisible(false);

    //设置标题不可见
    m_plot->legend->setVisible(false);
    m_plot->legend->setFont(QFont("Cantarell", 3));
}

void PointPlot::InitHistogram(HistogramType type)
{
    //直方图的区域大小
    m_plot->setGeometry(-10, 10, m_xWidth, m_yHeight-5);
    m_yLabel->setGeometry(0, 6, YLABEL_WIDTH, YLABEL_HEIGHT);
    m_yLabel->setAlignment(Qt::AlignCenter);
    m_xLabel->setGeometry(m_xWidth-25, m_yHeight-30, YLABEL_WIDTH, YLABEL_HEIGHT);
    m_xLabel->setAlignment(Qt::AlignCenter);
    m_topLabel->setGeometry(m_plot->width()/2, 0, TOPLABEL_WIDTH, TOPLABEL_HEIGHT);
    m_topLabel->setAlignment(Qt::AlignLeft);


    //设置坐标轴颜色
    m_plot->xAxis->setBasePen(QPen(QColor(Qt::white), 1));
    m_plot->yAxis->setBasePen(QPen(QColor(Qt::white), 1));


    //设置刻尺颜色
    m_plot->xAxis->setTickPen(QPen(QColor(Qt::white), 1));
    m_plot->yAxis->setTickPen(QPen(QColor(Qt::white), 1));
    m_plot->xAxis->setSubTickPen(QPen(QColor(Qt::white), 1));
    m_plot->yAxis->setSubTickPen(QPen(QColor(Qt::white), 1));


    //设置坐标轴标签
    QVector<double> ticks;
    QVector<QString> labels;

    if(type == HgT_RBC){
        ticks<<0<<100<<200<<300;
        labels<<"0"<<"100"<<"200"<<"300";
        m_plot->xAxis->setRange(0, 320);
    }
    else if(type == HgT_PLT){
        ticks<<0<<10<<20<<30<<40;
        labels<<"0"<<"10"<<"20"<<"30"<<"40";
        m_plot->xAxis->setSubTickLength(5);
        m_plot->xAxis->setRange(0, 40);
    }

    m_plot->xAxis->setAutoTicks(false);
    m_plot->xAxis->setTickVector(ticks);
    m_plot->xAxis->setTickVectorLabels(labels);

    m_plot->xAxis->setSubTickCount(1);
    m_plot->xAxis->setTickLength(0, 5);
    m_plot->xAxis->setSubTickLength(0,3);

    m_plot->xAxis->setBasePen(QPen(Qt::white));
    m_plot->xAxis->setTickPen(QPen(Qt::white));
    m_plot->xAxis->setTickLabelColor(Qt::white);
    m_plot->xAxis->setLabelColor(Qt::white);

    //设置y轴刻度值不可见
    m_plot->yAxis->setTicks(false);
    m_plot->yAxis->setTickLabels(false);

    //设置网格不可见
    m_plot->xAxis->grid()->setVisible(false);
    m_plot->xAxis2->grid()->setVisible(false);
    m_plot->yAxis->grid()->setVisible(false);
    m_plot->yAxis2->grid()->setVisible(false);

    //设置标题不可见
    m_plot->legend->setVisible(false);
}

void PointPlot::replot()
{
   m_plot->replot();
}


